# class Person:
#     def __init__(self, name, age):
#         self.name = name
#         self.age = age
#     @staticmethod                        #静态
#     def speak(self):
#         print(self.name, 'is speaking')
#     @classmethod                          #类
#     def talk(cls,obj):
#         print(obj.name, 'is talking')
#     @property
#     def walk(self):
#         print(self.name, 'is walking')
#     @walk.setter
#     def walk(self, num):
#         print(self.name, 'is', num)
#     @walk.deleter
#     def walk(self):
#         print(self.age, 'is not my age')
# p = Person('yyt',22)
# p.speak(p)                        #静态调用
# Person.talk(p)                    #类调用
# p.walk                           #属性调用
# p.walk = 'M'                     #属性调用增加参数
# del p.walk


# class Person:
#     def __init__(self, name, age):
#         self.name = name
#         self.age = age
# def speak(self):
#     print(self.name, 'is speaking')
# p = Person('yyt',22)
# setattr(p, 'sex', 'M')                #通过反射添加给p实例一个性别男
# print(p.sex)
# setattr(Person, 'speak', speak)       #通过反射给类添加一个speak函数
# p.speak()


# import sys
# class Dog:
#     def __init__(self, name):
#         self.name = name
# d = Dog('alex')
# this_mo = sys.modules[__name__]              #路径选择到本文件
# print(this_mo)
# print(hasattr(this_mo, 'Dog'))              #判断本文件是否有Dog类


# import importlib
# importlib.import_module()            #动态导入指定模块


# class Person:
#     lis = []
#     ins = None
#     def __init__(self, name, age):
#         self.name = name
#         self.age = age
#     def add(self, job):
#         self.lis.append(job)
#         print('%s收到工作%s到打印机，总工作数%s' % (self.name, job, len(self.lis)))
#     def __new__(cls, *args, **kwargs):                  #单例模式
#         if cls.ins is None:
#             obj = object.__new__(cls)
#             cls.ins = obj
#         return cls.ins
# p1 = Person('111', 33)
# p2 = Person('222', 33)
# p3 = Person('333', 33)
# p1.add('dayin1')
# p2.add('dayin2')
# p3.add('dayin3')


# class Person:
#     def __init__(self, name):
#         self.name = name
#     def __getitem__(self, item):          #查key
#         print(self.__dict__[item])
#     def __setitem__(self, key, value):     #增加key，value
#         self.__dict__[key] = value
#     def __delitem__(self, key):          #del p[key]  方式删除
#         self.__dict__.pop(key)
#     def __delattr__(self, item):         #del p.key   方式删除
#         self.__dict__.pop(item)
# try:
#     p = Person('yyt')
#     p.__getitem__('name')
#     p.__setitem__('sex', 'M')
#     p.__getitem__('sex')
#     del p.sex
#     p.__getitem__('sex')
# except KeyError as e:
#     print('找不到key的值了')


# def walk(self):
#     print(self.name, 'is walking')
# def talk(self):
#     print(self.name, 'is talking')
# def __init__(self, name):
#     self.name =name                                     #用type动态创建了一个类，包括构造函数和其他函数
# person = type('Person', (), {'__init__': __init__, 'walk': walk, 'talk': talk})
# p1 = person('yyt')
# print(p1.name)
# p1.talk()
# p1.walk()


# class NewworkError(BaseException):         #自定义一个错误继承BaseException
#     def __init__(self, msg):
#         self.msg = msg
#     def __str__(self):
#         return self.msg
# while 1:
#     try:
#         a = input('>:').strip()
#         b = input('>:').strip()
#         num = a + b
#         raise NewworkError('网络错误，请检查网络')       #主动触发异常  异常为自定义异常括号内容为自定义显示的内容必须填写
#     except NewworkError as e:                    #在try内的代码错误就会执行 可捕捉错误自定义输出
#         print(e, ',请仔细检查')
#     else:                               #try内代码正常run没有报错就执行else
#     finally:                             #无论什么情况都要执行一遍finally










